package com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.widget.PullRefreshLayout;
import com.superzqb.asus.Api.CampusShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Adapter.CampusShopAllAdapter;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.Adapter.CommentAdapter;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.Bean.CommentBean;
import com.superzqb.asus.superzqbUtil.GeneralFun;
import com.superzqb.asus.superzqbUtil.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static com.superzqb.asus.Api.ApiParam.maxItem;

public class CampusShopComment extends Activity implements View.OnClickListener{
    private ImageView back;
    private TextView title;
    private Context context;
    private ListView lv;
    private PullRefreshLayout layout;
    private int lastItem;
    private int listSize;
    private int pagenum=1;
    private List<CommentBean> listAll=new ArrayList<>();
    private CommentAdapter adapter;
    private Intent intent;
    private int gid;

    private boolean isButtom=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.campus_shop_comment);

        initView();
        adapter = new CommentAdapter(context, listAll);
        lv.setAdapter(adapter);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        mClear();
                        getLv();
                    }
                }, 2000);
            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    if (lastItem == listSize) {
                        if(!isButtom) {
                            pagenum++;
                         /*   Toast.makeText(context,
                                    "加载第" + pagenum + "页",
                                    Toast.LENGTH_SHORT).show();*/

                            getLv();
                        }else
                            Toast.makeText(context,
                                    "没有更多了",
                                    Toast.LENGTH_SHORT).show();

                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = lv.getLastVisiblePosition();

            }
        });
    }

    private void initView() {
        intent=getIntent();
        gid=Integer.valueOf(intent.getStringExtra("gid"));
        context=CampusShopComment.this;
        back=(ImageView)findViewById(R.id.back);
        title=(TextView)findViewById(R.id.title);
        title.setText("评论列表");
        layout= (PullRefreshLayout)findViewById(R.id.swipeRefreshLayout);
        lv=(ListView)findViewById(R.id.listView);

        back.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            default:break;
        }
    }

    public void getLv() {
        Retrofit retrofit=new GeneralFun().gsonCreate();
        CampusShopApi api=retrofit.create(CampusShopApi.class);
        Call<List<CommentBean>> call=api.getCommentList(pagenum,gid);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                if(response.isSuccessful()){
                    List<CommentBean> tmp=response.body();
                    if(tmp.size()!=0) {
                        listAll.addAll(tmp);
                        listSize = listAll.size() - 1;
                        adapter.setList(listAll);
                        adapter.notifyDataSetChanged();
                        if(tmp.size()!=maxItem)
                            isButtom=true;
                    }else{
                        isButtom=true;
                    }

                }
            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable throwable) {

              //  Toast.makeText(context,throwable.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });

    }
    @Override
    public void onResume() {
        super.onResume();
        mClear();
        getLv();
    }
    private void mClear() {
        listAll.clear();
        listSize=0;
        pagenum=1;
        isButtom=false;
    }
}
